import 'dart:convert';
import 'package:love_newf/app/com/base/L.dart';
import 'package:love_newf/app/com/base/key_mix.dart';
import 'package:love_newf/app/com/model/user_info_model.dart';
import 'package:love_newf/app/com/plugin/message_plugin.dart';
import 'package:love_newf/generated/l10n.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:love_newf/app/com/base/app.dart';
import 'package:love_newf/app/com/http/http_request.dart';
import 'package:love_newf/app/com/http/Res.dart';
import 'package:love_newf/app/com/model/login_user_info_model.dart';

class UserService {
  Future<LoginUserInfoModel?> login({phone, password, code}) async {
    Res r = await HttpRequest().postRes('/service/login/sms-code-login'.hx,
        {'username'.hx: phone, 'smsCode'.hx: code},
        loadingText: S.current.loading);
    if (r.success) {
      LoginUserInfoModel userInfoModel = LoginUserInfoModel();
      SpUtils()
          .setStorage("loginUserInfo", json.encode(userInfoModel.toJson()));
      return userInfoModel;
    } else {
      ToastUtils.toast(r.msg);
      return null;
    }
  }

  Future<UserInfoModel?> getUserInfo(
      {userId, loadingText = "加载中..."}) async {
    UserInfoModel? info = await HttpRequest().post<UserInfoModel>(
        '/api/user/getUser', {'userId': userId??0},
        loadingText: loadingText);
    if(info != null){
      L.userInfo = info;
    }
    return info;
   }
}
